#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <syslog.h>
int main()
{
    int sock, connected, bytes_recieved , true = 1;
    char recv_data[254];
    struct sockaddr_in server_addr,client_addr;
    int sin_size;
    //FILE *f;

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("Socket");
        exit(1);
    }
    if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1) {
        perror("Setsockopt");
        exit(1);
    }
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(5000);
    server_addr.sin_addr.s_addr = INADDR_ANY;
    bzero(&(server_addr.sin_zero),8);
    if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
        perror("Unable to bind");
        exit(1);
    }
    if (listen(sock, 5) == -1) {
        perror("Listen");
        exit(1);
    }
    fflush(stdout);

    while(1)
    {
        sin_size = sizeof(struct sockaddr_in);
        connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);
        //f = fopen("/var/log/keys.log","w");
        //printf("received conn");
        while(bytes_recieved = recv(connected, recv_data,254,0))
        {

            recv_data[bytes_recieved] = '\0';

            //printf("%s", recv_data);

            openlog("keys", LOG_PID, LOG_AUTHPRIV);
            syslog(LOG_INFO, "%s", recv_data);

            fflush(stdout);
            close(sock);
        }
    }
    //fclose(f);
    return 0;
}
